/**   
 * @Title: WxShopIntercept.java
 * @Package com.wx.interceptor
 * @Description: 
 * @author wangxiao
 * @date 2017年12月25日 下午3:45:05
 */
package com.wx.interceptor;

import java.util.HashMap;
import java.util.Map;

import com.eova.aop.AopContext;
import com.eova.aop.MetaObjectIntercept;
import com.eova.common.Easy;
import com.eova.common.utils.time.TimestampUtil;
import com.jfinal.kit.JMap;
import com.jfinal.kit.PathKit;
import com.jfinal.log.Log;
import com.jfinal.template.Engine;
import com.wx.model.WxAccount;
import com.wx.model.WxShop;
import com.wx.service.ShopService;
import com.xiaoleilu.hutool.http.HttpUtil;
import com.xiaoleilu.hutool.json.JSONUtil;

/**   
 * @ClassName: WxShopIntercept
 * @Description: 
 * @author wangxiao
 * @date 2017年12月25日 下午3:45:05
 */
public class WxShopOrderIntercept extends MetaObjectIntercept{

	
	private static String GET_ADD_XY = "http://apis.map.qq.com/ws/geocoder/v1/?address=【ADD】&output=json&key=【KEY】";
	
	private static String GET_ADD_KEY ="WPDBZ-DX3CD-WEY4I-HH5Z6-TP2VZ-6CBPX";
	static final ShopService shopService = ShopService.me;
	static Log log = Log.getLog(WxShopOrderIntercept.class);
	/**
	 * 查询前置任务(DIY复杂查询条件)
	 * 
	 * <pre>
	 * 用法：获取查询条件值
	 * ac.ctrl.getPara("query_查询条件字段名");
	 *
	 * 用法：追加条件
	 * ac.condition = "and id < ?";
	 * ac.params.add(999);
	 *
	 * 用法：覆盖条件
	 * ac.where = "where id < ?";
	 * ac.params.add(5);
	 *
	 * 用法：覆盖排序
	 * ac.sort = "order by id desc";
	 * 
	 * 用法：覆盖整个SQL语句
	 * ac.sql = "select * from table";
	 * </pre>
	 */
   @Override
    public void queryBefore(AopContext ac) throws Exception {
    }
   /**
	 * 新增前置任务(事务内)
	 * 
	 * <pre>
	 * ac.record 当前操作数据
	 * -------------
	 * 用法：自动赋值
	 * ac.record.set("create_time", TimestampUtil.getNow());
	 * ac.record.set("create_uid", ac.UID);
	 *
	 * 用法：唯一值判定
	 * int count = Db.queryInt("select count(*) from users where name = ?", ac.record.getStr("name"));
	 * if (count > 0) {
	 *     return Easy.error("名字不能重复");
	 * }
	 * </pre>
	 */
   @Override
public String addBefore(AopContext ac) throws Exception {
       //保存前判断用户输入地址和家乐福店铺地址距离
	   WxShop ws = WxShop.dao.findById(ac.record.getInt("shopid"));
	   WxAccount wx = WxAccount.dao.findById(ws.getAccountId());
	   Double dd =shopService.getXyFrom(ws.getLAT()+","+ws.getLNG(),ac.record.getStr("receive_pro")+ac.record.getStr("receive_city")+ac.record.getStr("receive_dis")+ac.record.getStr("receive_add"));
	   if(dd>10000){
		   return Easy.error("收件地址距离门店超过5公里，无法配送!");
	   }else{
		   ac.record.set("send_add", ws.getADDRESS());
		   ac.record.set("send_pro", ws.getPRO());
		   ac.record.set("send_city", ws.getCITY());
		   ac.record.set("send_dis", ws.getDIS());
		   ac.record.set("send_mobile", ws.getContactTel());
		   ac.record.set("send_name", ws.getCONTACT());
		   ac.record.set("appid", wx.getAppid());
		   ac.record.set("userid", "-1");
		   ac.record.set("create_time", TimestampUtil.getNow());
		   ac.record.set("status", 0);
		   String str = this.getTempMess(ac.record, wx.getOrderTempKey());
			Map paramMap = new HashMap<>();
			//	    paramMap.put("Constant_id", "createOrder");
			paramMap.put("content", str);
			paramMap.put("callback", "");
			String response = HttpUtil.post(wx.getAddOrderUrl(), paramMap);
			Map result = new HashMap<>();
			log.info(response);
			result = JSONUtil.parseObj(response.substring(1, response.length()-1));
				if("SUCCESS".equals(result.get("returnType"))){
					return null;
				}else{
					return Easy.error((String) result.get("returnValue"));
				}
			}
	   }
	public String getTempMess(Object paramMap,String key) {
		//根据参数获取对应模板
		JMap data = JMap.create();
		data.set("data", paramMap);
		Engine.use().setBaseTemplatePath(PathKit.getRootClassPath());
		String str="";
		try{
			str =Engine.use().getTemplate(key).renderToString(data);
		}catch (Exception e) {
			// TODO: handle exception
			str = "ERR";
			throw e;
		}finally{
			return str;
		}
	}
   
}
